//
//  MyCarViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/25.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "MyCarViewController.h"
#import "HYSegmentedControl.h"
#import "SelectedBtn.h"
#import "UPTableViewController.h"
#import "DownTableViewController.h"
#import "BottomBar.h"
#import "IndexNavViewController.h"
#define KanmiTime 0.3

@interface MyCarViewController ()<UIScrollViewDelegate,HYSegmentedControlDelegate,BottomBarBtnClickDelegate>
{
    BOOL _isedting;
}
@property (nonatomic,strong) HYSegmentedControl *hysegment;
@property (nonatomic,strong) BottomBar *toolbar;
@property (nonatomic,strong) BottomBar *secodebar;
@property (nonatomic,strong) UIScrollView *scrollview;
@end

@implementation MyCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.hysegment=[[HYSegmentedControl alloc]initWithOriginY:64 Titles:@[@"上架的车源",@"下架的车源"] delegate:self];
    [self.view addSubview:self.hysegment];
    self.navigationItem.title=@"我的车源";
    [self setupview];
    [self setupcontroller];
    if (self.isFromSUS) {
        NSMutableArray *vcarray=[[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-2];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-3];
        self.navigationController.viewControllers = vcarray;
    }
    // Do any additional setup after loading the view from its nib.
}
-(void)hySegmentedControlSelectAtIndex:(NSInteger)index{
 
    [self.scrollview setContentOffset:CGPointMake(XHScreenwidth*index, 0) animated:YES];
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    IndexNavViewController *vc=self.navigationController;
    [vc setupNavBar:NO];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat x=scrollView.contentOffset.x;
    NSLog(@"%f",x);
    if (x/XHScreenwidth==1) {
        [self.hysegment changeSegmentedControlWithIndex:1];
        
    }else{
        [self.hysegment changeSegmentedControlWithIndex:0];
       
    }
}


-(void)setupcontroller{
    UPTableViewController *upvc=[[UPTableViewController alloc]initWithStyle:UITableViewStylePlain];
    upvc.view.frame=CGRectMake(0, 0, XHScreenwidth, XHScreenheight-100);
    upvc.view.backgroundColor=bgGarycolor;
    [self.scrollview addSubview:upvc.view];
    [self addChildViewController:upvc];
    DownTableViewController *downvc=[[DownTableViewController alloc]initWithStyle:UITableViewStylePlain];
    downvc.view.frame=CGRectMake(XHScreenwidth, 0, XHScreenwidth, XHScreenheight-100);
   downvc.view.backgroundColor=bgGarycolor;
    [self.scrollview addSubview:downvc.view];
    [self addChildViewController:downvc];
    self.scrollview.delegate=self;
    BottomBar *bar=[[BottomBar alloc]initWithBtnTitleArray:@[@"下架",@"刷新"] WithDelegate:self:YES];
    [bar showInView:self.scrollview];
    self.toolbar=bar;
    self.toolbar.delegate=upvc;
    self.toolbar.datasource=upvc;
    upvc.bottombar=self.toolbar;
    bar.tag=0;
    BottomBar *downbar=[[BottomBar alloc]initWithBtnTitleArray:@[@"上架",@"删除"] WithDelegate:self :YES];
    downbar.x=XHScreenwidth;
    [downbar showInView:self.scrollview];
    self.secodebar=downbar;
    downbar.tag=1;
    self.secodebar.delegate=downvc;
     self.secodebar.datasource=downvc;
    downvc.bottombar=self.secodebar;
    
}
-(void)BottomBar:(BottomBar *)bottombar otherbtnClick:(NSInteger)index{
    NSLog(@"%ld",bottombar.tag);
}

-(void)rightclick{
     _isedting=!_isedting;
    [self setupRight:_isedting];
    [self.toolbar switchView];
    [self.secodebar switchView];
   
}
-(void)setupRight:(BOOL)iseidting{
    
    NSMutableDictionary *userinfo=[NSMutableDictionary dictionary];
    if (iseidting) {
        UIBarButtonItem *rightitem=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(rightclick)];
       
        self.navigationItem.rightBarButtonItem=rightitem;
        userinfo[@"iseiting"]=@(1);
    }else{
        self.toolbar.btnenable=NO;
        self.secodebar.btnenable=NO;
        UIBarButtonItem *rightitem=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(rightclick)];
        userinfo[@"iseiting"]=@(0);
        self.navigationItem.rightBarButtonItem=rightitem;
    }
    NSNotification *notice=[[NSNotification alloc]initWithName:NoticeIsEidt object:nil userInfo:userinfo];
    [[NSNotificationCenter defaultCenter] postNotification:notice];

}

-(void)setupview{
    //初始化导航栏右侧按钮
    [self setupRight:NO];
    //初始化scrollview
    UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, XHScreenwidth, XHScreenheight-100)];
  
    scrollview.backgroundColor=[UIColor redColor];
    scrollview.contentSize=CGSizeMake(XHScreenwidth*2, XHScreenheight-100);
    [self.view addSubview:scrollview];
    self.scrollview=scrollview;
    self.scrollview.pagingEnabled=YES;
    self.scrollview.showsHorizontalScrollIndicator=NO;
    self.scrollview.showsVerticalScrollIndicator=NO;
    self.scrollview.bounces=NO;


}
-(void)toolbarmenuclick:(UIButton *)btn{
    NSLog(@"btntag---%ld",btn.tag);
}

-(void)postcar{
    NSLog(@"发车源");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
